﻿#include "stdafx.h"
#include "Cube.h"

Cube::Cube():Objet(){}

Cube::Cube(Point c, Vecteur n1, Vecteur n2, float s)
{
	centre = c;
	centre.normalize();
	side = s;

	n1.normalize();
	n2.normalize();
	Vecteur n3 = n1.produitVectoriel(n2);
	

	Point p1(centre, side/2, n1);
	Point p2(centre, -side/2, n1);
	Point p3(p1, side/2, n2);
	Point p4(p1, -side/2, n2);
	Point p5(p2, side/2, n2);
	Point p6(p2, -side/2, n2);

	sommets[0] = Point(p3, side/2, n3);
	sommets[1] = Point(p3, -side/2, n3);
	sommets[2] = Point(p4, -side/2, n3);
	sommets[3] = Point(p4, side/2, n3);
	sommets[4] = Point(p5, side/2, n3);
	sommets[5] = Point(p5, -side/2, n3);
	sommets[6] = Point(p6, -side/2, n3);
	sommets[7] = Point(p6, side/2, n3);

	Point distanceSide2[3];
	Point distanceSide[3];
	int m = 0;
	int n = 0;

	for(int i = 1 ; i < 8 ; i++)
	{
		if(sommets[0].distance(sommets[i]) < side + 0.001 
			&& sommets[0].distance(sommets[i]) > side - 0.001)
		{
			distanceSide[n] = sommets[i];
			n++;
		}
		else if(sommets[0].distance(sommets[i]) < sqrt(2.0)*side + 0.001 
			&& sommets[0].distance(sommets[i]) > sqrt(2.0)*side - 0.001)
		{
			distanceSide2[m] = sommets[i];
			m++;
		}
	}

	triangles[0] = Triangle(sommets[0], distanceSide[0],distanceSide[1]);
	triangles[1] = Triangle(sommets[0], distanceSide[0],distanceSide[2]);
	triangles[2] = Triangle(sommets[0], distanceSide[1],distanceSide[2]);

	n = 0;
	for(int i = 1 ; i < 8 ; i++)
	{
		if(distanceSide2[0].distance(sommets[i]) < side + 0.001 && distanceSide2[0].distance(sommets[i]) > side - 0.001)
		{
			distanceSide[n] = sommets[i];
			n++;
		}
	}

	triangles[3] = Triangle(distanceSide2[0], distanceSide[0],distanceSide[1]);
	triangles[4] = Triangle(distanceSide2[0], distanceSide[0],distanceSide[2]);
	triangles[5] = Triangle(distanceSide2[0], distanceSide[1],distanceSide[2]);

	n = 0;
	for(int i = 1 ; i < 8 ; i++)
	{
		if(distanceSide2[1].distance(sommets[i]) < side + 0.001 && distanceSide2[1].distance(sommets[i]) > side - 0.001)
		{
			distanceSide[n] = sommets[i];
			n++;
		}
	}

	triangles[6] = Triangle(distanceSide2[1], distanceSide[0],distanceSide[1]);
	triangles[7] = Triangle(distanceSide2[1], distanceSide[0],distanceSide[2]);
	triangles[8] = Triangle(distanceSide2[1], distanceSide[1],distanceSide[2]);

	n = 0;
	for(int i = 1 ; i < 8 ; i++)
	{
		if(distanceSide2[2].distance(sommets[i]) < side + 0.001 && distanceSide2[2].distance(sommets[i]) > side - 0.001)
		{
			distanceSide[n] = sommets[i];
			n++;
		}
	}

	triangles[9] = Triangle(distanceSide2[2], distanceSide[0],distanceSide[1]);
	triangles[10] = Triangle(distanceSide2[2], distanceSide[0],distanceSide[2]);
	triangles[11] = Triangle(distanceSide2[2], distanceSide[1],distanceSide[2]);

}

Cube::Cube(Cube const& cube)
{
	centre = cube.centre;
	side = cube.side;

	for(int i = 0; i < 8; i++)
		sommets[i] = cube.sommets[i];

	for(int i = 0; i < 12; i++)
		triangles[i] = cube.triangles[i];

}

Cube::~Cube(){}

Cube& Cube::operator=(Cube const& cube)
{
	centre = cube.centre;
	side = cube.side;

	for(int i = 0; i < 8; i++)
		sommets[i] = cube.sommets[i];

	for(int i = 0; i < 12; i++)
		triangles[i] = cube.triangles[i];

	return *this;
}

bool Cube::intersection(Rayon r)
{
	Intersection interTemp;

	bool flag = false;

	for(int i = 0; i < 12 ; i++)
	{
		if(triangles[i].intersection(r) && interTemp.getT() > triangles[i].getIntersection().getT())
		{
			flag = true;
			interTemp = triangles[i].getIntersection();
			interTemp.setObjet(this);
		}
	}

	inter = interTemp;
	return flag;
	
}

BoiteEnglobante Cube::getBoite()
{
	BoiteEnglobante b;
    
	b.corner1.setX(centre.getX() - side/2);
	b.corner1.setY(centre.getY() - side/2);
	b.corner1.setZ(centre.getZ() - side/2);

	b.corner2.setX(centre.getX() + side/2);
	b.corner2.setY(centre.getY() + side/2);
	b.corner2.setZ(centre.getZ() + side/2);	
    
	return b;
}

bool Cube::aBoite()
{
	return true;
}

